package com.changggou.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author: zhengke
 * @date: 2020/12/11
 * @return:
 * @description:
 */

@SpringBootApplication
@EnableEurekaClient
public class SystemGateWayApplication {
    public static void main(String[] args) {
        SpringApplication.run(SystemGateWayApplication.class,args);
    }

    //定义一个keyResolver,用来计算某一个类型的限流的key，也就是说通过keyResolver来指定限流的key
    @Bean
    public KeyResolver ipKeyResolver(){
        return  new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                //根据用户额客户端IP进行限流
                return Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
            }
        };
    }
}
